import { defineStore } from 'pinia';
import { ref } from 'vue';

//用户中心页数据存储
export const userStore = defineStore('userStore', () => {

  const userInfoData = ref()

  const userPostsList = ref()

  const userAnglingList = ref()

  const userAnglingCollectsList = ref()

  const setPosts = (data)=>{
    userPostsList.value = data
  }

  const setAngling = (data)=>{
    userAnglingList.value = data
  }

  const setCollectAngling = (data)=>{
    userAnglingCollectsList.value = data
  }

  const setUserInfo = (data1:object,data2:Array<object>,data3:Array<object>,data4:Array<object>)=>{
    userInfoData.value = data1

    userPostsList.value = data2

    userAnglingList.value = data3

    userAnglingCollectsList.value = data4
  }

  const updateUserInfo = (newData: any) => {
    userInfoData.value = newData;
  };

  const removePost = (postId: string) => {
    if (userPostsList.value) {
      const index = userPostsList.value.findIndex(post => post.postId === postId);
      if (index > -1) {
        userPostsList.value.splice(index, 1);
      }
    }
  };

  const updatePostStatus = (postId: string, newStatus: string) => {
    if (userPostsList.value) {
      const post = userPostsList.value.find(post => post.postId === postId);
      if (post) {
        post.releaseFlag = newStatus;
      }
    }
  };

  return{
    userInfoData,
    userPostsList,
    userAnglingList,
    userAnglingCollectsList,
    setUserInfo,
    setPosts,
    setAngling,
    setCollectAngling,
    updateUserInfo,
    removePost,
    updatePostStatus,
  }
});

export default userStore;
